package cn.jn.lhm.acl.service.impl;

import cn.jn.lhm.acl.entity.Permission;
import cn.jn.lhm.acl.entity.RolePermission;
import cn.jn.lhm.acl.mapper.PermissionMapper;
import cn.jn.lhm.acl.mapper.RolePermissionMapper;
import cn.jn.lhm.acl.service.PermissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author LHM
 * @Tips 热爱每一天，奋斗每一天
 * @date 2020/6/22 - 20:21
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Resource
    private PermissionMapper permissionMapper;

    @Resource
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public List<Permission> getAllInfo() {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        List<Permission> list = permissionMapper.selectList(wrapper);
        return recursionResult(list);
    }

    @Override
    public void deletePermissionById(String id) {
        ArrayList<String> idList = new ArrayList<>();
        selectChildrenIdList(id, idList);
        idList.add(id);
        permissionMapper.deleteBatchIds(idList);
    }

    @Override
    public void doAssign(String roleId, String[] permissionId) {
        Arrays.stream(permissionId).forEach(per -> {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId).setPermissionId(per);
            rolePermissionMapper.insert(rolePermission);
        });
    }

    /**
     * 根据当前的菜单的Id，递归查询子菜单的Id，封装到集合中
     * @param id 当前菜单的Id
     * @param idList 所有要删除的菜单的集合
     */
    private void selectChildrenIdList(String id, ArrayList<String> idList) {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", id);
        wrapper.select("id");
        List<Permission> childList = permissionMapper.selectList(wrapper);
        if (childList.size() > 0){
            childList.forEach(child -> {
                idList.add(child.getId());
                selectChildrenIdList(child.getId(), idList);
            });
        }
    }

    /**
     * 递归组装所有的数据
     *
     * @param list 全部的数据
     * @return 组装后的数据
     */
    private List<Permission> recursionResult(List<Permission> list) {
        ArrayList<Permission> result = new ArrayList<>();
        //获取最顶层的数据
        list.stream().filter(per -> per.getPid().equals("0")).forEach(per -> {
            per.setLevel(1);
            result.add(recursionProcess(per, list));
        });

        return result;
    }


    /**
     * 递归体
     *
     * @param per  顶层菜单
     * @param list 所有菜单数据
     * @return 顶层菜单
     */
    private Permission recursionProcess(Permission per, List<Permission> list) {
        if (per == null || list.stream().noneMatch(p -> p.getPid().equals(per.getId()))) {
            return per;
        }
        per.setChildren(new ArrayList<Permission>());
        list.stream().filter(p -> p.getPid().equals(per.getId())).forEach(p -> {
                p.setLevel(per.getLevel() + 1);
                per.getChildren().add(recursionProcess(p , list));
            });
        return per;

/*        per.setChildren(new ArrayList<Permission>());
        for (Permission permission: list){
            if (permission.getPid().equals(per.getId())){
                permission.setLevel(per.getLevel() + 1);
                per.getChildren().add(recursionProcess(permission, list));
            }
        }

        return per;*/
    }


}
